package org.gnuklub;

import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.DialogInterface;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.Phones;

import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import android.widget.Button;
import android.widget.TextView;
import java.util.regex.*;

public class Glavna extends Activity {
	public static final String PREFS_NAME = "ringer.preferences";
	private static final int MENU_QUIT = 0;
	private static final int MENU_SETTINGS = 1;
	private static final int MENU_SETTINGS_COUNTRY = MENU_SETTINGS + 1;
	private static final int MENU_SETTINGS_AREA = MENU_SETTINGS + 2;

	public static final int PICK_CONTACT = 3;
	private static final int DIALOG_MAIN = 0;
	private static final int DIALOG_SETTINGS = 1;

	private String countryCode, areaCode, prefixNo, intlPrefixNo;
	private Button btnContacts;
	private Button btnDial;
	private String phoneNo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		// default values treba da se uzmu iz locales
		countryCode = settings.getString("contryCode", "381");
		areaCode = settings.getString("areaCode", "18");
		prefixNo = settings.getString("prefixNo", "0");
		intlPrefixNo = settings.getString("intlPrefixNo", "00");

		btnContacts = (Button) findViewById(R.id.btn_contacts);
		btnDial = (Button) findViewById(R.id.btn_dial);
		btnContacts.setText(R.string.btn_search);
		btnDial.setText(R.string.btn_call);
		btnDial.setEnabled(false);

		btnContacts.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(Intent.ACTION_PICK,
						Phones.CONTENT_URI);
				startActivityForResult(intent, PICK_CONTACT);
			}
		});

		btnDial.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				DTMF dialer = new DTMF(Glavna.this);
				dialer.playNumber(phoneNo);
			}
		});
	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);

		switch (reqCode) {
		case (PICK_CONTACT):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = managedQuery(contactData, null, null, null, null);
				if (c.moveToFirst()) {
					phoneNo = c.getString(c
							.getColumnIndexOrThrow(Phones.NUMBER));
					// convert the number by regex
					btnDial.setText(R.string.btn_call);
					btnDial.append("\n ");
					btnDial.append(phoneNo);
					btnDial.setEnabled(true);
				}
			}
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_QUIT, 0, R.string.menu_quit);
		// menu.add(0, MENU_SETTINGS, 0,
		// R.string.menu_settings).setCheckable(true);
		SubMenu subMenu = menu.addSubMenu(0, MENU_SETTINGS, 1,
				R.string.submenu_settings_title);
		// ove zemlje treba da se povlace iz resursa, i imena i njihova lista.
		// takodje treba da se pri inicijaciji odredi default zemlja
		// na osnovu locale
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "Serbia").setChecked(true);
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "Macedonia");
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "UK");
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "USA/North America");
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "Germany");
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "Austria");
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "Australia");
		subMenu.add(99, MENU_SETTINGS_COUNTRY, 0, "Custom");// ovo lansira
									
		
		// dialog
		menu.add(0, MENU_SETTINGS_AREA, 0, "Area code");
		subMenu.setGroupCheckable(99, true, true);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case MENU_SETTINGS_AREA:
			// AlertDialog.Builder builder = new AlertDialog.Builder(this);
			// builder.setMessage("Sranje od dialoga... TODO.")
			// .setCancelable(false)
			// .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			// public void onClick(DialogInterface dialog, int id) {
			// dialog.cancel();
			// }
			// })
			// .setNegativeButton("No", new DialogInterface.OnClickListener() {
			// public void onClick(DialogInterface dialog, int id) {
			// dialog.cancel();
			// }
			// });
			// AlertDialog alert = builder.create();
			// alert.show();
			showDialog(0);
			return true;
		case MENU_SETTINGS_COUNTRY:
			
		case MENU_QUIT:
			this.finish();
			return true;
		}
		return false;
	}

	@Override
	protected void onStop() {
		super.onStop();
		// save settings
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("areaCode", areaCode);
		editor.putString("countryCode", countryCode);
		editor.putString("prefixNo", prefixNo);
		editor.putString("intlPrefixNo", intlPrefixNo);
		editor.commit();
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
		case DIALOG_MAIN:
			LayoutInflater factory = LayoutInflater.from(this);
			final View noteView = factory.inflate(R.layout.settings, null);
			
			TextView poruka = (TextView) noteView.findViewById(R.id.poruka);
			poruka.setText("Hello");
			dialog = new AlertDialog.Builder(this)
					.setTitle(R.string.menu_settings).setView(noteView)
					.setPositiveButton("blah",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {
									
								}
							}).setNegativeButton("Cancel", null)
					.create();
			break;
		case DIALOG_SETTINGS:
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Are you sure you want to exit?").setCancelable(
					false).setPositiveButton("Yes",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {

						}
					}).setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							dialog.cancel();
						}
					});
			dialog = builder.create();
			break;
		default:
			dialog = null;
		}
		return dialog;
	}

}
